package service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import dao.GoodsDAO;
import dao.MyCartDAO;
import entity.Criteria;
import entity.Goods;
import entity.Logistics;
import entity.MyCart;
import service.GoodsService;
import service.MyCartService;

@Component //创建对象
public class MyCartServiceImpl implements MyCartService{

	@Autowired//注入
	private MyCartDAO dao;

	@Override
	public void insertMyCart(MyCart cart) {
		// TODO Auto-generated method stub
		dao.insertMyCart(cart);
	}

	@Override
	public List<MyCart> selectMyCart(int uid, Integer start, Integer offset) {
		// TODO Auto-generated method stub
		return dao.selectMyCart(uid, start, offset);
	}

	@Override
	public Integer getCount(Integer uid) {
		// TODO Auto-generated method stub
		return dao.getCount(uid);
	}

	@Override
	public Double getCartTotalPrice(int uid) {
		//通过dao查询当前用户购物车总价
		return dao.getCartTotalPrice(uid);
	}

	@Override
	public boolean checkOutCart(int uid) {
		// 查询当前购物车的商品
		List<MyCart> carts=dao.selectCartByUid(uid);
		//循环购物车数据，在循环中添加物流表
		//购物车    	 mycart   uid gid photo gname gprice count
		//物流	logistics uid gid orderId lstatus pagAmout photo
		
		
		//随机生成订单编号,订单号原始表使用了int类型
		int orderId=(int)((Math.random())*100000000);
		for(MyCart mc:carts){
			//创建logistics对象
			Logistics logis=new Logistics();
			logis.setGid(mc.getGid());
			logis.setUid(mc.getUid());
			logis.setOrderId(orderId);
			logis.setPayAmount(Double.parseDouble(mc.getGprice()+""));
			logis.setPhoto(mc.getGphoto());
			//结算购物车则订单状态为1 	未发货
			logis.setLstatus(1);
			//在循环中插入数据
			dao.insertlogistics(logis);
		}
		//删除uid购物车
		//dao.deleteCartUid(uid);
		
		return true;
	}
	
	

}
